//
//  HERportReadHeadCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/25.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HERportReadHeadCell.h"
#import <SDCycleScrollView.h>
#import "HEShufCell.h"

@interface HERportReadHeadCell ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *loopView;
@property (nonatomic, strong) NSArray *loopArr;
@property (nonatomic, strong) UIImageView *backView;

@property (nonatomic, strong) UILabel *titleLb; 

@end

@implementation HERportReadHeadCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = CLightGrayColor;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    _backView = [UIImageView new];
    _backView.image = IMAGE_NAMED(@"health_guide_bg");
//    _backView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_backView];
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView);
        make.right.left.equalTo(self.contentView);
        make.height.offset(242);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
    _loopArr = @[@"鲁**", @"黄**", @"朱**", @"范**", @"欧**", @"林**", @"闫**", @"沈**", @"张**"];
    // 如果要实现自定义cell的轮播图，必须先实现customCollectionViewCellClassForCycleScrollView:和 setupCustomCell:forIndex:代理方法
    _loopView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 10, 200, 28) delegate:self placeholderImage: nil];
    _loopView.scrollDirection = UICollectionViewScrollDirectionVertical;
        _loopView.showPageControl = NO;
    _loopView.imageURLStringsGroup = _loopArr;
    _loopView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:.2];
    [_loopView hg_setCornerOnRightWithRadius:15];
    [_backView addSubview:_loopView];
    
    _titleLb = [UILabel new];
    _titleLb.text = @"2191081人解读";
    _titleLb.font = DYBlodFont(18);
    _titleLb.textColor = CNavBgColor;
    _titleLb.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_titleLb];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(134);
        make.left.equalTo(self.contentView).mas_offset(30);
        make.right.equalTo(self.contentView).mas_offset(-30);
    }];
    [_titleLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    
    NSArray *checks = @[@"● 1000+全职职业医师", @"● 1000+全职职业医师", @"● 提供有效健康改善方案"];

    NSMutableArray *btnArr = [NSMutableArray array];
    
    //存放上一个button
    UIButton *buttonBefore;
    //存放button距离左侧的宽度，来确定是否需要换行
    __block CGFloat right = 0.0;
    //间距
    CGFloat margin = 15.f;
    for(int i = 0; i < checks.count; i++) {
        UILabel *backBtn = [UILabel new];
        backBtn.font = DYFont(13);
        backBtn.numberOfLines = 0;
        [_backView addSubview:backBtn];
        backBtn.text = checks[i];
        backBtn.textColor = CNavLightBlackColor;
        [btnArr addObject:backBtn];
//        backBtn.backgroundColor = UIColor.orangeColor;
        
        CGFloat fitWidth = (KScreenWidth - 30 - 30) / 2.0;

        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(fitWidth);
//            make.height.mas_equalTo(20);
            if(i==0){
              make.left.equalTo(self.contentView).offset(margin + 15);
              make.top.equalTo(_titleLb.mas_bottom).offset(10);
              right = margin + fitWidth + margin;
            }else{
              if(right + fitWidth > KScreenWidth){
                  make.top.mas_equalTo(buttonBefore.mas_bottom).offset(10);
                  make.left.equalTo(self.contentView).offset(margin + 15);
                  right =  margin+fitWidth+margin;
              }else{
                  make.left.mas_equalTo(buttonBefore.mas_right).offset(margin);
                  make.top.equalTo(buttonBefore);
                  right += fitWidth+margin;
              }
            }
            
            if (i == (checks.count -1)) {
//                make.bottom.equalTo(self.contentView).mas_offset(-10);
            }
        }];
        buttonBefore = backBtn;
    }
    
}


// 不需要自定义轮播cell的请忽略下面的代理方法
// 如果要实现自定义cell的轮播图，必须先实现customCollectionViewCellClassForCycleScrollView:和setupCustomCell:forIndex:代理方法
- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view
{
    if (view != _loopView) {
        return nil;
    }
    return [HEShufCell class];
}

- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view
{
    HEShufCell *myCell = (HEShufCell *)cell;
    myCell.title = [NSString stringWithFormat:@"%@刚刚解读了报告", _loopArr[index]];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
